﻿using System;
using System.ComponentModel;
using Notoric.Gui.Windows;
using System.Collections.Generic;

namespace Notoric.Gui.Commands
{
    /// <summary>
    /// Implements a command for inserting a new music section to the active page.
    /// </summary>
    public class InsertMusicSectionCommand : RibbonCommand
    {
        #region Constructors

        /// <summary>
        /// Creates a new <see cref="InsertMusicSectionCommand"/> instance.
        /// </summary>
        /// <param name="mainWindow">
        /// <para>
        /// Type: <see cref="MainWindow"/>
        /// </para>
        /// <para>
        /// Reference to the application's main window.
        /// </para>
        /// </param>
        /// <exception cref="ArgumentNullException">
        /// The <paramref name="mainWindow"/> is <see langword="null"/>.
        /// </exception>
        public InsertMusicSectionCommand(MainWindow mainWindow)
            : base(mainWindow)
        {
            ScreenTipTitle = "Insert a Music Section";
            ScreenTipText = "Creates a new music section in the current page that will display a selected part of a selected composition.";
            DisableReason = "A page must be open first.";

            mainWindow.PropertyChanged += new PropertyChangedEventHandler(mainWindow_PropertyChanged);
        }

        #endregion

        #region ICommand Members

        /// <inheritdoc/>
        public override bool CanExecute(object parameter)
        {
            return ApplicationWindow.ActivePage != null;
        }

        /// <inheritdoc/>
        public override void Execute(object parameter)
        {
            var dlg = new MusicSectionPropertiesDialog();
            dlg.Compositions = ApplicationWindow.ProjectExplorer.Projects[0].GetRootDirectory(ViewModel.Elements.RootDirectory.Compositions);
            if (dlg.ShowDialog() == true)
            {
                var cmd = new ViewModel.NewMusicSectionCommand(ApplicationWindow.ActivePage.UndoStack, ApplicationWindow.ActivePage.RedoStack)
                {
                    Composition = dlg.SelectedComposition,
                    FirstMeasure = dlg.FirstMeasure,
                    LastMeasure = dlg.LastMeasure,
                    Owner = ApplicationWindow.ActivePage,
                    Parameter = dlg.MeasuresPerSystem,
                    PresentedParts = new List<ViewModel.Elements.PartDescriptor>()
                };
                foreach (var part in dlg.SelectedParts)
                    cmd.PresentedParts.Add(part);

                var appState = ((App)App.Current).CurrentState;
                appState.MouseTarget = typeof(Controls.Page);
                appState.Command = cmd;
                appState.StatusBarMessage = "Draw the area where you want to put the section.";
            }
        }

        #endregion

        #region Event Handlers

        /// <summary>
        /// Occurs when a property of the main application window has changed.
        /// </summary>
        /// <param name="sender">Object that has sent the event</param>
        /// <param name="e">Event data</param>
        private void mainWindow_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "ActivePage")
                RaiseCanExecuteChanged();
        }

        #endregion
    }
}
